package edu.gyc.producer.consumer;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ProtectiveCloth{
    private int id;
    private String type;

    public ProtectiveCloth(int id, String type) {
        this.id = id;
        this.type = type;
    }

    @Override
    public String toString() {
        return "防护服{" +
                "id=" + id +
                ", type='" + type + '\'' +
                '}';
    }
}

 class ProtectiveClothFactory {
    private Integer stockNum=0;
    private static int clothid=0;
    private List<ProtectiveCloth> cloths = new ArrayList<>();
     private Lock lock=new ReentrantLock();
     private Condition condition=lock.newCondition();
    public  void put() {
        lock.lock();
        try {
            while (stockNum>=10){
                condition.await();
            }
          //  Thread.sleep(100);
            clothid++;
            ProtectiveCloth cloth=new ProtectiveCloth(clothid,"医用防护服");
            cloths.add(cloth);
            stockNum++;
            System.out.println(Thread.currentThread().getName()+"生产 "+cloth+" 库存 "+stockNum);
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }
    public  void get() {

        lock.lock();
        try {
            while (stockNum<=0){
                condition.await();
            }
          //  Thread.sleep(200);
            ProtectiveCloth cloth= cloths.remove(0);
            stockNum--;
            System.out.println(Thread.currentThread().getName()+"获得 "+cloth+" 库存 "+stockNum);
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }



}

public class ClothTest {
    public static void main(String[] args) {
        ProtectiveClothFactory clothFactory=new ProtectiveClothFactory();
        new Thread(()->{
            for (int i = 0; i <26; i++) {

                clothFactory.put();
            }
        },"生产者1").start();
        new Thread(()->{
            for (int i = 0; i < 30; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                clothFactory.put();
            }
        },"生产者2").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {

                clothFactory.get();
            }
        },"消费者1").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {

                clothFactory.get();
            }
        },"消费者2").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {

                clothFactory.get();
            }
        },"消费者3").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {

                clothFactory.get();
            }
        },"消费者4").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {

                clothFactory.get();
            }
        },"消费者5").start();


    }


}

